package com.endes.android.model.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.endes.android.entidades.SSEC01;
import com.endes.android.entidades.SSEC1C;
import com.endes.android.model.database.EndesOpenHelper;

public class QSSec1Dao {
	private EndesOpenHelper endesOpenHelper;
	private SQLiteDatabase db;
	private Cursor cursor = null;
	private Context context;

	public QSSec1Dao(Context context) {
		this.context = context;
		endesOpenHelper = new EndesOpenHelper(context);

	}

	public SSEC01 ObtenerSeccion1(int qsCluster, int qsNumber, int qsHome, 
			int numeroOrdenPersonaSeleccionada, SSEC01 ssec01) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC01 where qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
						new String[] { qsCluster + "", qsNumber + "", qsHome + "", 
								numeroOrdenPersonaSeleccionada + ""});
		if (cursor.moveToFirst()) {
			ssec01.setQs100(cursor.getInt(cursor.getColumnIndex("qs100")));	
			if(cursor.isNull(cursor.getColumnIndex("qs101")) && cursor.isNull(cursor.getColumnIndex("qs101CX"))
				&& cursor.isNull(cursor.getColumnIndex("qs101X"))){
				ssec01.setQs101(null);	
				ssec01.setQs101CX(null);	
				ssec01.setQs101X(null);	
			} else{
				ssec01.setQs101(cursor.getInt(cursor.getColumnIndex("qs101")));	
				ssec01.setQs101CX(cursor.getString(cursor.getColumnIndex("qs101CX")));	
				ssec01.setQs101X(cursor.getString(cursor.getColumnIndex("qs101X")));	
			}
			ssec01.setQs102(cursor.getInt(cursor.getColumnIndex("qs102")));	
			if(cursor.isNull(cursor.getColumnIndex("qs103U")) && cursor.isNull(cursor.getColumnIndex("qs103C"))){
				ssec01.setQs103U(null);	
				ssec01.setQs103C(null);
				ssec01.setQs104(null);	
				ssec01.setQs105(null);	
				ssec01.setQs106(null);	
			} else{
				ssec01.setQs103U(cursor.getInt(cursor.getColumnIndex("qs103U")));	
				ssec01.setQs103C(cursor.getInt(cursor.getColumnIndex("qs103C")));
				ssec01.setQs104(cursor.getInt(cursor.getColumnIndex("qs104")));
				if(cursor.isNull(cursor.getColumnIndex("qs105")) && cursor.isNull(cursor.getColumnIndex("qs106"))){
					ssec01.setQs105(null);	
					ssec01.setQs106(null);	
				} else{
					ssec01.setQs105(cursor.getInt(cursor.getColumnIndex("qs105")));	
					ssec01.setQs106(cursor.getInt(cursor.getColumnIndex("qs106")));	
				}
				
			}
			ssec01.setQs107(cursor.getInt(cursor.getColumnIndex("qs107")));	
			if(cursor.isNull(cursor.getColumnIndex("qs108")) && cursor.isNull(cursor.getColumnIndex("qs108CX"))
					&& cursor.isNull(cursor.getColumnIndex("qs108X"))){
				ssec01.setQs108(null);
				ssec01.setQs108CX(null);	
				ssec01.setQs108X(null);
			} else{
				ssec01.setQs108(cursor.getInt(cursor.getColumnIndex("qs108")));
				ssec01.setQs108CX(cursor.getString(cursor.getColumnIndex("qs108CX")));	
				ssec01.setQs108X(cursor.getString(cursor.getColumnIndex("qs108X")));
			}
			ssec01.setQs109(cursor.getInt(cursor.getColumnIndex("qs109")));	
			if(cursor.isNull(cursor.getColumnIndex("qs110U")) && cursor.isNull(cursor.getColumnIndex("qs110C"))){
				ssec01.setQs110U(null);	
				ssec01.setQs110C(null);	
			} else{
				ssec01.setQs110U(cursor.getInt(cursor.getColumnIndex("qs110U")));	
				ssec01.setQs110C(cursor.getInt(cursor.getColumnIndex("qs110C")));	
			}
			ssec01.setQs111(cursor.getInt(cursor.getColumnIndex("qs111")));
			if(cursor.isNull(cursor.getColumnIndex("qs112")) && cursor.isNull(cursor.getColumnIndex("qs113"))){
				ssec01.setQs112(null);	
				ssec01.setQs113(null);
			} else{
				ssec01.setQs112(cursor.getInt(cursor.getColumnIndex("qs112")));	
				ssec01.setQs113(cursor.getInt(cursor.getColumnIndex("qs113")));
			}
		}
		Cerrar();
		return ssec01;
	}

	public void guardarSec1Encuesta(SSEC01 ssec01) {
		ContentValues contenido = new ContentValues();
		contenido.put("qs100", ssec01.getQs100());
		contenido.put("qs101", ssec01.getQs101());
		contenido.put("qs101CX", ssec01.getQs101CX());
		contenido.put("qs101X", ssec01.getQs101X());
		contenido.put("qs102", ssec01.getQs102());
		contenido.put("qs103U", ssec01.getQs103U());
		contenido.put("qs103C", ssec01.getQs103C());
		contenido.put("qs104", ssec01.getQs104());
		contenido.put("qs105", ssec01.getQs105());
		contenido.put("qs106", ssec01.getQs106());
		contenido.put("qs107", ssec01.getQs107());
		contenido.put("qs108", ssec01.getQs108());
		contenido.put("qs108CX", ssec01.getQs108CX());
		contenido.put("qs108X", ssec01.getQs108X());
		contenido.put("qs109", ssec01.getQs109());
		contenido.put("qs110U", ssec01.getQs110U());
		contenido.put("qs110C", ssec01.getQs110C());
		contenido.put("qs111", ssec01.getQs111());
		contenido.put("qs112", ssec01.getQs112());
		contenido.put("qs113", ssec01.getQs113());

		db = endesOpenHelper.getWritableDatabase();
		db.update(
				"SSEC01",
				contenido,
				"qsCluster=? and qsNumber=? and qsHome=? and qs20C=?",
				new String[] { ssec01.getQsCluster() + "",
						ssec01.getQsNumber() + "", ssec01.getQsHome() + "",
						ssec01.getQs20C() + ""});
		db.close();
	}
	
	/*public int existeRegistroCaratula(int conglomerado, int vivienda, int hogar) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSEC1C where qsCluster=? and qsNumber=? and qsHome=?",
						new String[] { conglomerado + "", vivienda + "",
								hogar + "" });
		return cursor.getCount();
	}

	public List<SSECNIN> ObtenerCaratulaParteNinios(int qsCluster,
			int qsNumber, int qsHome) {
		List<SSECNIN> listSsecNin = null;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSECNIN where qsCluster=? and qsNumber=? and qsHome=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "" });
		if (cursor.moveToFirst()) {
			listSsecNin = new ArrayList<SSECNIN>();
			while (!cursor.isAfterLast()) {
				SSECNIN obj = new SSECNIN();
				obj.setQsCluster(qsCluster);
				obj.setQsNumber(qsNumber);
				obj.setQsHome(qsHome);
				obj.setQsOrdNin(cursor.getInt(cursor.getColumnIndex("qsOrdNin")));
				obj.setQsResNin(cursor.getInt(cursor.getColumnIndex("qsResNin")));
				listSsecNin.add(obj);
				cursor.moveToNext();
			}
		}
		Cerrar();
		return listSsecNin;
	}

	public void guardarCaratulaParteNinios(SSECNIN ssecnin) {
		ContentValues contenido = new ContentValues();
		contenido.put("qsOrdNin", ssecnin.getQsOrdNin());
		contenido.put("qsResNin", ssecnin.getQsResNin());
		if (ExisteSSECNIN((ssecnin))) {
			db = endesOpenHelper.getWritableDatabase();
			db.update("SSECNIN", contenido,
					"qsCluster=? and qsNumber=? and qsHome=?", new String[] {
							ssecnin.getQsCluster() + "",
							ssecnin.getQsNumber() + "",
							ssecnin.getQsHome() + "" });
			db.close();
		} else {
			db = endesOpenHelper.getWritableDatabase();
			contenido.put("qsCluster", ssecnin.getQsCluster());
			contenido.put("qsNumber", ssecnin.getQsNumber());
			contenido.put("qsHome", ssecnin.getQsHome());
			db.insert("SSECNIN", null, contenido);
			db.close();
		}
	}

	public boolean ExisteSSECNIN(SSECNIN ssecnin) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSECNIN where qsCluster=? and qsNumber=? and qsHome=?",
						new String[] { ssecnin.getQsCluster() + "",
								ssecnin.getQsNumber() + "",
								ssecnin.getQsHome() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}

	public SSECVISIT ObtenerCaratulaParteVisitas(int qsCluster, int qsNumber,
			int qsHome, int qsNumeroOrden, String fechaActual) {
		SSECVISIT ssecvisit = null;
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSECVISIT where qsCluster=? and qsNumber=? and qsHome=? and qsNumero=? and qsFechaVisit<=?",
						new String[] { qsCluster + "", qsNumber + "",
								qsHome + "", qsNumeroOrden + "", fechaActual});
		if (cursor.moveToFirst()) {
			ssecvisit = new SSECVISIT();
			ssecvisit.setQsCluster(qsCluster);
			ssecvisit.setQsNumber(qsNumber);
			ssecvisit.setQsHome(qsHome);
			ssecvisit.setQsNumeroOrden(qsNumeroOrden);
			ssecvisit.setQsFechaVisit(cursor.getString(cursor
					.getColumnIndex("qsFechaVisit")));
			ssecvisit.setQsNombre(cursor.getString(cursor
					.getColumnIndex("qsNombre")));
			ssecvisit.setQsResult(cursor.getInt(cursor
					.getColumnIndex("qsResult")));
			ssecvisit.setQsFechaProxVisit(cursor.getString(cursor
					.getColumnIndex("qsFechaProxVisit")));
			ssecvisit.setQsHoraProxVisit(cursor.getString(cursor
					.getColumnIndex("qsHoraProxVisit")));
			cursor.moveToNext();
		}
		Cerrar();
		return ssecvisit;
	}

	public void guardarCaratulaParteVisitas(SSECVISIT ssecvisit) {
		ContentValues contenido = new ContentValues();
		contenido.put("qsFechaVisit", ssecvisit.getQsFechaVisit());
		contenido.put("qsNombre", ssecvisit.getQsNombre());
		contenido.put("qsResult", ssecvisit.getQsResult());
		contenido.put("qsFechaProxVisit", ssecvisit.getQsFechaProxVisit());
		contenido.put("qsHoraProxVisit", ssecvisit.getQsHoraProxVisit());
		if (ExisteSSECVISIT((ssecvisit))) {
			db = endesOpenHelper.getWritableDatabase();
			db.update(
					"SSECVISIT",
					contenido,
					"qsCluster=? and qsNumber=? and qsHome=? and qsNumero=?",
					new String[] { ssecvisit.getQsCluster() + "",
							ssecvisit.getQsNumber() + "",
							ssecvisit.getQsHome() + "",
							ssecvisit.getQsNumeroOrden() + ""});
			db.close();
		} else {
			db = endesOpenHelper.getWritableDatabase();
			contenido.put("qsCluster", ssecvisit.getQsCluster());
			contenido.put("qsNumber", ssecvisit.getQsNumber());
			contenido.put("qsHome", ssecvisit.getQsHome());
			contenido.put("qsNumero", ssecvisit.getQsNumeroOrden());
			db.insert("SSECVISIT", null, contenido);
			db.close();
		}

	}

	public boolean ExisteSSECVISIT(SSECVISIT ssecvisit) {
		db = endesOpenHelper.getReadableDatabase();
		cursor = db
				.rawQuery(
						"Select * from SSECVISIT where qsCluster=? and qsNumber=? and qsHome=?",
						new String[] { ssecvisit.getQsCluster() + "",
								ssecvisit.getQsNumber() + "",
								ssecvisit.getQsHome() + "" });
		if (cursor.moveToFirst()) {
			Cerrar();
			return true;
		} else {
			Cerrar();
			return false;
		}
	}*/

	public void Cerrar() {
		cursor.close();
		db.close();
	}

}
